package com.mycc.game.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mycc.game.damin.Information;
import com.mycc.game.damin.Npc;
import com.mycc.game.dao.NpcDao;
import com.mycc.game.service.NpcService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NpcServiceImpl implements NpcService {
    @Autowired
    private NpcDao Npcdao;


    //查询所有npc
    @Override
    public IPage<Npc> selAllNpc(int pageNum, int pageSize) {
        //创建查询容器
        Page<Npc> page = new Page<>(pageNum, pageSize);
        return Npcdao.selectPage(page, null);
    }

    //通过id查询npc
    @Override
    public IPage<Npc> selAllNpcById(int id, int pageNum, int pageSize) {
        //创建查询容器
        LambdaQueryWrapper<Npc> qew = new LambdaQueryWrapper<>();
        qew.eq(Npc::getNpcid, id);
        Page<Npc> page = new Page<>(pageNum, pageSize);
        //条件
        return Npcdao.selectPage(page, qew);
    }

    //通过名字查询npc
    @Override
    public IPage<Npc> selAllNpcByName(String name, int pageNum, int pageSize) {
        LambdaQueryWrapper<Npc> qew = new LambdaQueryWrapper<>();
        qew.like(Npc::getName, name);
        Page<Npc> page = new Page<>(pageNum, pageSize);
        return Npcdao.selectPage(page, qew);
    }


    //删除npc
    @Override
    public boolean delNpc(int id) {
        int x = Npcdao.deleteById(id);
        if (x > 0) {
            return true;
        } else {
            return false;
        }
    }

    //增加npc
    @Override
    public boolean addNpc(Npc npc) {
        int x = Npcdao.insert(npc);
        if (x > 0) {
            return true;
        } else {
            return false;
        }
    }

    //修改npc
    @Override
    public boolean updNpc(int id, Npc npc) {
        Npc npcs = Npcdao.selectById(id);
        if (npcs != null) {
            BeanUtils.copyProperties(npc, npcs);
            int x = Npcdao.updateById(npcs);
            return x > 0;
        }
        return false;
    }
}


